const path = require('path');
const fs = require('fs')
const jwt = require('jsonwebtoken');


function authtoken (req, res, next) {
    const token = req.cookies.token;
    if (token) {
        const cert = fs.readFileSync(path.resolve(__dirname, '../secret/jwt_pub.pem'));
        jwt.verify(token, cert, (err, decoded) =>  {
            // token失效
            if (err) {
                res.send({
                    code:0,
                    msg:"身份失效"
                })
            } else {
                // 放行
                // req.userinfo = decoded;
                next();
            }
        });
        // 未登陆
    } else {
        res.send({
            code:0,
            msg:"登录失败"
        })
    }
}

exports.AuthToken= authtoken